package com.snack.shop.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.shop.common.Result;
import com.snack.shop.entity.MerchantMessage;
import com.snack.shop.service.MerchantMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 商家消息控制器
 */
@Tag(name = "商家消息管理")
@RestController
@RequestMapping("/merchant/message")
@RequiredArgsConstructor
@SaCheckRole("merchant")
public class MerchantMessageController {

    private final MerchantMessageService merchantMessageService;

    @Operation(summary = "分页查询商家消息")
    @GetMapping("/page")
    public Result<Page<MerchantMessage>> page(
            @Parameter(description = "消息类型（可选）") @RequestParam(required = false) Integer type,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size
    ) {
        Long merchantId = StpUtil.getLoginIdAsLong();
        Page<MerchantMessage> page = merchantMessageService.pageByMerchant(merchantId, type, current, size);
        return Result.success(page);
    }

    @Operation(summary = "获取未读消息数")
    @GetMapping("/unread-count")
    public Result<Integer> getUnreadCount() {
        Long merchantId = StpUtil.getLoginIdAsLong();
        Integer count = merchantMessageService.getUnreadCount(merchantId);
        return Result.success(count);
    }

    @Operation(summary = "标记单条消息已读")
    @PostMapping("/{id}/read")
    public Result<Void> markRead(@PathVariable Long id) {
        Long merchantId = StpUtil.getLoginIdAsLong();
        merchantMessageService.markRead(merchantId, id);
        return Result.success();
    }

    @Operation(summary = "全部标记已读")
    @PostMapping("/read-all")
    public Result<Void> markAllRead() {
        Long merchantId = StpUtil.getLoginIdAsLong();
        merchantMessageService.markAllRead(merchantId);
        return Result.success();
    }
}
